package com.kyc.kycxkplaygame.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import com.kyc.kycxkplaygame.R;
import com.kyc.kycxkplaygame.util.SharedPreferencesUtils;

public class WelcomeActivity extends AppCompatActivity {

    /*private ImageView welcome_fruit;
    private ImageView welcome_fruit_xiyou;
    private ImageView welcome_fruit_putao;
    private ImageView welcome_fruit_bg;*/
    private Animation animation_bg;
    private Intent tiliIntent;
    //private ProgressBar welcome_progressbar;

    public String[] testProp = {"体力", "葡萄", "草莓", "西柚", "青苹果", "西瓜", "提示", "炸弹", "时间"};

    private ImageView login_btn;
    public static WelcomeActivity instance;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        instance = this;

        String oldtime = (String) SharedPreferencesUtils.getParam(WelcomeActivity.this, "oldtime", "");
        String tili_num = (String) SharedPreferencesUtils.getParam(WelcomeActivity.this, "tili_num", "");

        login_btn = findViewById(R.id.login_btn);


        String phone = (String) SharedPreferencesUtils.getParam(WelcomeActivity.this, "phone", "");
        String pwd = (String) SharedPreferencesUtils.getParam(WelcomeActivity.this, "pwd", "");
        if (!TextUtils.isEmpty(phone) && !TextUtils.isEmpty(pwd)) {
            login_btn.setVisibility(View.GONE);
            startActivity(new Intent(WelcomeActivity.this, LevelActivity.class));
            WelcomeActivity.this.finish();
        } else {
            login_btn.setVisibility(View.VISIBLE);
        }
        login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //进入登录界面 TODO
                //startActivityForResult(new Intent(WelcomeActivity.this, LoginActivity.class), 1101);
                startActivity(new Intent(WelcomeActivity.this,LoginActivity.class));
                //WelcomeActivity.this.finish();
            }
        });

       /* welcome_fruit = findViewById(R.id.welcome_fruit);
        welcome_fruit_xiyou = findViewById(R.id.welcome_fruit_xiyou);
        welcome_fruit_putao = findViewById(R.id.welcome_fruit_putao);

        welcome_fruit_bg = findViewById(R.id.welcome_fruit_bg);

        welcome_progressbar = findViewById(R.id.welcome_progressbar);
        welcome_progressbar.setMax(100);
        welcome_progressbar.setProgress(0);*/

       /* if (TextUtils.isEmpty(oldtime)) {
            SharedPreferencesUtils.setParam(WelcomeActivity.this, "tili_num", "5");
        } else {

        }*/

        //animation_bg = AnimationUtils.loadAnimation(WelcomeActivity.this, R.anim.welcome_fruit_bg);
        // slideview(-300,0);
    }




    /*  public void slideview(final float p1, final float p2) {
        TranslateAnimation animation = new TranslateAnimation(0, 0, p1, p2);
        animation.setInterpolator(new BounceInterpolator());
        animation.setDuration(2000);
        animation.setStartOffset(0);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                welcome_fruit_bg.startAnimation(animation_bg);
                welcome_progressbar.setProgress(20);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                int left = welcome_fruit.getLeft();
                int top = (int) (welcome_fruit.getTop()+(p2 - p1));
                int width = welcome_fruit.getWidth();
                int height = welcome_fruit.getHeight();
                welcome_fruit.clearAnimation();
                welcome_fruit_bg.clearAnimation();
                welcome_fruit.setVisibility(View.GONE);
                welcome_fruit_putao.setVisibility(View.VISIBLE);
                welcome_progressbar.setProgress(40);
                putaoeview(-300,0);
            }
        });
        welcome_fruit.startAnimation(animation);
    }

    public void putaoeview(final float p1, final float p2) {
        TranslateAnimation animation = new TranslateAnimation(0, 0, p1, p2);
        animation.setInterpolator(new BounceInterpolator());
        animation.setDuration(2000);
        animation.setStartOffset(0);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                welcome_fruit_bg.startAnimation(animation_bg);
                welcome_progressbar.setProgress(60);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                int left = welcome_fruit_putao.getLeft();
                int top = (int) (welcome_fruit_putao.getTop()+(p2 - p1));
                int width = welcome_fruit_putao.getWidth();
                int height = welcome_fruit_putao.getHeight();
                welcome_fruit_putao.clearAnimation();
                welcome_fruit_bg.clearAnimation();
                welcome_fruit_putao.setVisibility(View.GONE);
                welcome_fruit_xiyou.setVisibility(View.VISIBLE);
                welcome_progressbar.setProgress(80);
                xiyoueview(-300,0);
            }
        });
        welcome_fruit_putao.startAnimation(animation);
    }

    public void xiyoueview(final float p1, final float p2) {
        TranslateAnimation animation = new TranslateAnimation(0, 0, p1, p2);
        animation.setInterpolator(new BounceInterpolator());
        animation.setDuration(2000);
        animation.setStartOffset(0);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                welcome_fruit_bg.startAnimation(animation_bg);
                welcome_progressbar.setProgress(100);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                int left = welcome_fruit_xiyou.getLeft();
                int top = (int) (welcome_fruit_xiyou.getTop()+(p2 - p1));
                int width = welcome_fruit_xiyou.getWidth();
                int height = welcome_fruit_xiyou.getHeight();
                welcome_fruit_xiyou.clearAnimation();
                welcome_fruit_bg.clearAnimation();
                startActivity(new Intent(WelcomeActivity.this, LevelActivity.class));
                WelcomeActivity.this.finish();
            }
        });
        welcome_fruit_xiyou.startAnimation(animation);
    }*/

    //@Override
    /*protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == 1101) {
            String status = data.getStringExtra("status");
            if (status.equals("success")) {
                String phone = data.getStringExtra("phone");
                String pwd = data.getStringExtra("pwd");

                final UserInfoGdBean userInfoGdBean = new UserInfoGdBean();
                userInfoGdBean.setUserphone(phone);
                userInfoGdBean.setUserpwd(pwd);
                int infoCount = UserInfoManager.getInstance().getUserInfoCount();
                userInfoGdBean.setId((long) (infoCount + 1));
                userInfoGdBean.setCode(1101);
                userInfoGdBean.setBatter(0);
                userInfoGdBean.setFraction(0);
                userInfoGdBean.setLevel("1");
                userInfoGdBean.setPropname("体力");
                userInfoGdBean.setPropnum(5);
                userInfoGdBean.setStarcount(0);

                final LevelPassBean levelPassBean = new LevelPassBean();
                int leveid = LevelInfoManager.getInstance().getLevelInfoCount();
                levelPassBean.setId(leveid + 1);
                levelPassBean.setLevel("0");
                levelPassBean.setStarCount(0);
                levelPassBean.setStatus("未通关");

                final List<PropInfoBean> list = new ArrayList<>();
                for (int i = 0; i < testProp.length; i++) {
                    PropInfoBean propInfoBean = new PropInfoBean();
                    int userInfo = PropInfoManager.getInstance().getPropInfoCount();
                    propInfoBean.setId(userInfo + 1 + i);
                    propInfoBean.setPropName(phone);
                    propInfoBean.setPropName(testProp[i]);
                    if (testProp[i].equals("体力")) {
                        propInfoBean.setPropNum(5);
                    } else {
                        propInfoBean.setPropNum(0);
                    }
                    list.add(propInfoBean);
                }

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        UserInfoManager.getInstance().insertUserInfo(userInfoGdBean);
                        LevelInfoManager.getInstance().insertLevleInfo(levelPassBean);
                        PropInfoManager.getInstance().insertPropInfo(list);
                    }
                }).start();

                SharedPreferencesUtils.setParam(WelcomeActivity.this, "phone", phone);
                SharedPreferencesUtils.setParam(WelcomeActivity.this, "pwd", pwd);
                startActivity(new Intent(WelcomeActivity.this, LevelActivity.class));
                WelcomeActivity.this.finish();
            } else if (status.equals("fail")) {

            } else if (status.equals("login")) {
                String phone = data.getStringExtra("phone");
                String pwd = data.getStringExtra("pwd");
                SharedPreferencesUtils.setParam(WelcomeActivity.this, "phone", phone);
                SharedPreferencesUtils.setParam(WelcomeActivity.this, "pwd", pwd);
                startActivity(new Intent(WelcomeActivity.this, LevelActivity.class));
                WelcomeActivity.this.finish();
            }
        }
    }*/
}
